home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.07 Jul 93 / Embedded Software / Test_vol_const.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-08  |  1.1 KB  |  39 lines  |  [TEXT/MPS ]

  1.  
  2. /* Test file to see how the compiler handles volatile and const
  3.  * keywords.
  4.  *
  5.  * Compile normally, then inspect the assembly output
  6.  * (using DumpObj or whatever) to see if
  7.  * REGISTER_ADDRESS is written to.  MPW 3.2 will not comply with
  8.  * this intended use.  Then, change the last line to 
  9.  *      *TheStrobe=1;
  10.  * and notice the difference in the assembly code.
  11.  */
  12.  
  13. #define REGISTER_ADDRESS  0x3000000
  14.  
  15. main() {
  16.  
  17.  /* RegStrobe is a type of register that is 8 bits wide.  It is
  18.   * a constant pointer to a volatile entity.
  19.   */
  20.  typedef unsigned char volatile * const RegStrobe;
  21.  
  22.  /* Obviously, you won't want to EXECUTE this program on your
  23.   * Mac since it will write to a hard-coded address, which 
  24.   * will only be polite in your target environment
  25.   */
  26.  RegStrobe TheStrobe = (RegStrobe) REGISTER_ADDRESS;
  27.  
  28.  /* whack the register
  29.   */
  30.  *TheStrobe;         /* Any compiler that optimizes this out is not
  31.                       * respecting your use of const and volatile
  32.               * keywords.  While not strictly a violation
  33.               * of the ANSI C standard, it is sub-optimal
  34.               * for purposes of embedded development.  Use
  35.               * caution!
  36.               */
  37. }
  38.  
  39.